<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );

class PropertiesModelPanel extends JModel
{
	var $_data;
	var $TableName = null;
	var $_total = null;
	var $_pagination = null;

	function __construct()
	{
		parent::__construct();
		global $mainframe, $option;

		$component = JComponentHelper::getComponent( 'com_properties' );
		$params = new JParameter( $component->params );
		$this->Mostrar = $params->get( 'cantidad_productos' ) ;
		$this->ProvinciaDefecto = $params->get( 'ProvinciaDefecto' ) ;

		if(!JRequest::getVar('limitstart')){
			$this->setState('limit', 10);
			$this->setState('limitstart', 0);
		}else{
			$limit = 10;
			$this->setState('limit', 10);
			$limitstart = JRequest::getVar('limitstart');
			$this->setState('limitstart', $limitstart);
			$start = JRequest::getVar('start');
			$this->setState('start', $start);
		}

	}


	function _buildQueryPanel()
	{
		$user =& JFactory::getUser();
		$this->_query = ' SELECT p.*,c.name as name_category,cy.name as name_country,s.name as name_state,l.name as name_locality,pf.name as name_profile,pf.logo_image as logo_image_profile, '
		. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
		. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
		. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
		. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sdslug,'
		. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug '
		. ' FROM #__properties_products AS p '
		. ' LEFT JOIN #__properties_category AS c ON c.id = p.parent '
		. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '
		. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
		. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
		. ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '
		//. ' WHERE p.published = 1 '
		. ' WHERE p.agent_id = '.$user->id
		//	. $this->sqlProvincia.' '
		//	. $this->sqlProvinciaDefecto.' '
		//	. $this->sqlCiudad.' '
		//	. $this->sqlSector.' '
		//	. $this->sqlCategoria.' '
		.' ORDER BY p.id DESC'
		;

		//	$querysl = ' SELECT p.*,lb.* FROM #__properties_products AS p LEFT JOIN  #__properties_lightbox AS lb ON p.agent_id = lb.uid WHERE lb.propid = p.id AND lb.uid = '.$user->id;
			
		//echo str_replace('#_','jos',$this->_query);
		return $this->_query;
	}


	function getItemid()
	{
		$query = 'SELECT id FROM #__menu' .
				' WHERE LOWER( link ) = "index.php?option=com_properties&view=properties"';

		$this->_db->setQuery( $query );
		$this->row = $this->_db->loadResult();

		return $this->row;
	}

	function getProductUser()
	{
		$user =& JFactory::getUser();
		$query = ' SELECT p.*,c.name as name_category,cy.name as name_country,s.name as name_state,l.name as name_locality, '
		. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
		. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
		. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
		. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sdslug,'
		. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug '
		. ' FROM #__properties_products AS p '
		. ' LEFT JOIN #__properties_category AS c ON c.id = p.parent '
		. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '
		. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
		. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
		//. ' WHERE p.published = 1 '
		. ' WHERE p.agent_id = '.$user->id;
			
		$this->_db->setQuery( $query );
		$this->_data = $this->_db->loadObject();
		//echo '$this->_id; '.$this->_id;
		//echo str_replace('#_','jos',$query);
		return $this->_data;
	}

	function getProductEdit()
	{
		$id=JRequest::getVar('id');
		$user =& JFactory::getUser();
		$query = ' SELECT p.*,c.name as name_category,cy.name as name_country,s.name as name_state,l.name as name_locality, '
		. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
		. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
		. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
		. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sdslug,'
		. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug '
		. ' FROM #__properties_products AS p '
		. ' LEFT JOIN #__properties_category AS c ON c.id = p.parent '
		. ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '
		. ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
		. ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
		//. ' WHERE p.published = 1 '
		. ' WHERE p.id = '.$id;
			
		$this->_db->setQuery( $query );
		$this->_data = $this->_db->loadObject();
		//echo '$this->_id; '.$this->_id;
		//echo str_replace('#_','jos',$query);
		return $this->_data;
	}





	function getDataAgent()
	{
		$Agent=JRequest::getVar('aid');
		// if data hasn't already been obtained, load it
		$queryA = 'SELECT * FROM #__properties_profiles ' .
			' WHERE mid = '.$Agent;

		$this->_db->setQuery( $queryA );
		$this->row = $this->_db->loadObject();

		return $this->row;
	}




	function getDataSearch()
	{
		// if data hasn't already been obtained, load it
		if (empty($this->_data)) {
			$query = $this->_buildQuerySearch();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}

	function getTotalSearch()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_total)) {
			$queryS = $this->_buildQuerySearch();
			$this->_total = $this->_getListCount($queryS);
		}
		return $this->_total;
	}

	function getPaginationSearch()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination)) {
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotalSearch(), $this->getState('limitstart'), $this->getState('limit') );

		}
		return $this->_pagination;
	}


	 







	function getDataPanel()
	{
		$querysl = $this->_buildQueryPanel();
		$this->_db->setQuery( $querysl );
		$this->_data = $this->_getList($querysl, $this->getState('limitstart'), $this->getState('limit'));
		return $this->_data;
	}

	function getTotalPanel()
	{
		if (empty($this->_total)) {
			$querysl = $this->_buildQueryPanel();
			// 	    $querysl = ' SELECT p.*,lb.* FROM #__properties_products AS p LEFT JOIN  #__properties_lightbox AS lb ON p.agent_id = lb.uid WHERE lb.propid = p.id AND lb.uid = '.$user->id;
				
			$this->_total = $this->_getListCount($querysl);
		}
		return $this->_total;
	}

	function getPaginationPanel()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination)) {
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotalPanel(), $this->getState('limitstart'), $this->getState('limit') );

		}
		return $this->_pagination;
	}


	function getProfile()
	{
		$user =& JFactory::getUser();
		$queryP = ' SELECT * FROM #__properties_profiles '.
					'  WHERE mid = '.$user->id;
		$this->_db->setQuery( $queryP );
		$this->_data = $this->_db->loadObject();

		return $this->_data;
			
	}
		
	function getCant_items()
	{
		$user =& JFactory::getUser();
		$queryT = ' SELECT COUNT(id) FROM #__properties_products '.
					'  WHERE agent_id = '.$user->id;
		$this->_db->setQuery( $queryT );
		$this->_data = $this->_db->loadResult();
		return $this->_data;
			
	}
		
}//fin clase